package com.web.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class PropertiesUtil {
	
	private static Map<String, Properties> propertiesMap = new HashMap<String, Properties>();
	/**
	 * For every properties, it will be only initialized once
	 * @param propertiesName the propertiesName is excepted to NOT have the .properties extension
	 * @return
	 * @throws IOException
	 */
	public static Properties load(String propertiesName) throws IOException{
		PropertiesUtil.class.getClassLoader();
		PropertiesUtil.class.getClassLoader();
		InputStream propertiesStream = PropertiesUtil.class.getResourceAsStream(propertiesName+".properties");
		
		if(propertiesMap.containsKey(propertiesName)){
			return propertiesMap.get(propertiesName);
		}
		Properties properties = new Properties();
		properties.load(propertiesStream);
		propertiesMap.put(propertiesName, properties);
		return properties;	
	}
	/**
	 * 
	 * @return the properties of "env.properties"
	 * @throws IOException 
	 */
	public static Properties getDefaultProperties() throws IOException{
		return load("/env");
	}
}
